package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.base.BaseShareService;
import com.tongzhan.consts.SysDictTypeKeyConst;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 活动
 * @author htmic
 * @date 2020/7/29
 */
@Service
public class ActionService extends BaseShareService {

    @Autowired
    private SysDictService sysDictService;

    /**
     * 判断活动名是否存在
     * @param name
     * @param type 类型 1是党派 2是侨台 3是乡贤
     * @return
     */
    public long findActionByName(String name,Integer type) {
        long count = actionCenterRepository.count(new Query().addCriteria(BaseCriteria.where("name").is(name).and("type").is(type).and("is_del").is(0)));
        return count;
    }

    /**
     * 添加
     * @param center
     * @return
     */
    public long addAction(ActionCenter center) {
        center.setIs_del(0);
        center.setIs_pass(0);
        center.setType(2);
        User userById = findUserById(center.getAdd_userid());
        if(userById!=null){
            center.setAdd_username(userById.getUserName());
        }
        center.setStreetCode(ThreadLocalUtil.getThreadLocal().getStreetCode());
        center.setVillageCode(ThreadLocalUtil.getThreadLocal().getVillageCode());
        long l = actionCenterRepository.insertSelective(center);
        if(l>0){
            //添加角标
            markPromptService.add(new MarkPrompt(2, center.getAid()),"21");
            return  1;
        }
        return 0;
    }

    /**
     * 详情
     * @param aid
     * @return
     */
    public ActionCenter actionDetail(String aid,String userId) {
        ActionCenter center = actionCenterRepository.findByPrimaryKey(aid);
        if (center != null) {
            center.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_ACTION_TYPE, center.getActionTypeId()));
        }
        markPromptService.read(aid,userId);
        return center;
    }

    /**
     * 活动列表
     * @param page
     * @param size
     * @param userId
     * @param auditStatus 审核状态(0: 未审核、1：通过，2：不通过)
     * @return
     */
    public List actionList(Integer page, Integer size,String userId,Integer auditStatus) {
        List<Map<String, Object>> list = new ArrayList<>();

        //侨台办
        Query qw = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("type").is(2).and("is_pass").is(auditStatus));
        PageRequest of = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionCenter> pageList1 = actionCenterRepository.findPage(qw, of);
        if (!CollectionUtils.isEmpty(pageList1.getContent())) {
            pageList1.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_ACTION_TYPE, item.getActionTypeId()));
            });
        }

        //侨联
        Query qw2 = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("type").is(2).and("is_pass").is(auditStatus));
        PageRequest of2 = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw2.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw2.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionCenter> pageList2 = actionCenterRepository.findPage(qw2, of2);
        if (!CollectionUtils.isEmpty(pageList2.getContent())) {
            pageList2.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_ACTION_TYPE, item.getActionTypeId()));
            });
        }

        //华侨
        Query qw3 = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("type").is(2).and("is_pass").is(auditStatus));
        PageRequest of3 = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw3.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw3.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionCenter> pageList3 = actionCenterRepository.findPage(qw3, of3);
        if (!CollectionUtils.isEmpty(pageList3.getContent())) {
            pageList3.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_ACTION_TYPE, item.getActionTypeId()));
            });
        }

        //乡镇
        Query qw4 = new Query().addCriteria(BaseCriteria.where("is_del").is(0).and("type").is(2).and("is_pass").is(auditStatus));
        PageRequest of4 = PageRequest.of(page, size, Sort.Direction.DESC, "time");
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.STREET_CODE)) {
            qw4.addCriteria(BaseCriteria.where("streetCode").like(ThreadLocalUtil.getThreadLocal().getStreetCode()));
        }
        if (ThreadLocalUtil.getThreadLocal().getDutiesCode().equals(ThreadLocalUtil.VILLAGE_CODE)) {
            qw4.addCriteria(BaseCriteria.where("villageCode").like(ThreadLocalUtil.getThreadLocal().getVillageCode()));
        }
        Page<ActionCenter> pageList4 = actionCenterRepository.findPage(qw4, of4);
        if (!CollectionUtils.isEmpty(pageList4.getContent())) {
            pageList4.getContent().forEach(item->{
                item.setActionTypeName(sysDictService.getValueByType(SysDictTypeKeyConst.QIAOTAI_ACTION_TYPE, item.getActionTypeId()));
            });
        }

        Map<String, Object> map1 = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map1.put("qtbList", pageList1.getContent());
        map1.put("total", pageList1.getTotalElements());
        map2.put("qlList", pageList2.getContent());
        map2.put("total", pageList2.getTotalElements());
        map3.put("hqList", pageList3.getContent());
        map3.put("total", pageList3.getTotalElements());
        map4.put("xzList", pageList4.getContent());
        map4.put("total", pageList4.getTotalElements());

        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        return list;
    }

    /**
     * 审核
     * @param id
     * @param type 审核状态(0: 未审核、1：通过，2：不通过)
     * @param userId 审核人id
     * @return
     */
    public long auditAction(String id, Integer type, String userId) {
        ActionCenter actionCenter = actionCenterRepository.findByPrimaryKey(id);
        if(actionCenter!=null){
            User user = findUserById(userId);
            if(user!=null){
                actionCenter.setAuditTime(new Date());
                actionCenter.setAuditUserId(userId);
                actionCenter.setAuditUserName(user.getUserName());
            }
            actionCenter.setIs_pass(type);//审核结果   0:未审核、1:同意 2:不同意
            long l = actionCenterRepository.updateByPrimaryKeySelective(actionCenter);
            if (l >= 0) {
                return 1;
            } else {
                return 0;
            }
        }
        return 0;
    }
}
